unit ufrmmain;

{$mode objfpc}{$H+}

interface

uses
  Classes,
  SysUtils,
  Forms,
  Controls,
  Graphics,
  Dialogs,
  inifiles,
  ExtCtrls,
  ComCtrls,
  Menus,
  StdCtrls,
  TreeFilterEdit;

type

  { Tfrmmain }

  Tfrmmain = class(TForm)
    imgLogo: TImage;
    ListView1: TListView;
    mmoLog: TMemo;
    mi17: TMenuItem;
    pnlMain: TPanel;
    Separator2: TMenuItem;
    mi16: TMenuItem;
    Separator1: TMenuItem;
    mi15: TMenuItem;
    mi14: TMenuItem;
    mi13: TMenuItem;
    mi12: TMenuItem;
    mi11: TMenuItem;
    mm: TMainMenu;
    pcDB: TPageControl;
    pc: TPageControl;
    pnlHead: TPanel;
    pnlLeft: TPanel;
    pmTable: TPopupMenu;
    Splitter1: TSplitter;
    sb: TStatusBar;
    Splitter2: TSplitter;
    tvDbFlt: TTreeFilterEdit;
    tvDB: TTreeView;
    tsDbList: TTabSheet;
    tsDB: TTabSheet;
    tsWelcome: TTabSheet;
    tvDBList: TTreeView;
    procedure FormCreate(Sender: TObject);
    procedure FormDropFiles(Sender: TObject; const FileNames: array of string);
    procedure tvDBDblClick(Sender: TObject);
  private

  public

  end;

var
  frmmain: Tfrmmain;


implementation

{$R *.lfm}

uses
  mormot.core.Data,
  ufrmtab,
  uFrmQry,
  uConst,
  udmsys;

var
  qryCnt: integer;

procedure Tfrmmain.FormDropFiles(Sender: TObject; const FileNames: array of string);
begin
  if High(FileNames) >= 0 then
    dmsys.loadDb(FileNames[0]);
end;

procedure Tfrmmain.tvDBDblClick(Sender: TObject);
var
  ts: TTabSheet;
  qry: TFrmQry;
  tab: Tfrmtab;
begin
  case tvdb.Selected.Level of
    1: if (pc.ActivePage.Controls[0] is tfrmqry) then
      begin
        with tfrmqry(pc.ActivePage.Controls[0]).se do
          if Lines.Strings[1].IndexOf(tvdb.Selected.Text) < 0 then
            Text := 'select *'#13#10 + '  from ' + tvdb.Selected.Text;
      end
      else
      begin
        ts := TTabSheet.Create(pc);
        ts.Caption := strDbQry + IntToStr(qryCnt);
        ts.PageControl := pc;
        ts.Visible := True;
        pc.ActivePage := ts;
        qry := TFrmQry.Create(ts);
        qry.Parent := ts;
        qry.Align := alClient;
        qry.Visible := True;
        qry.se.Text := 'select *'#13#10 + '  from ' + tvdb.Selected.Text;
        Inc(qryCnt);
      end;
    2: if (pc.ActivePage.Controls[0] is tfrmqry) then
      begin
        with tfrmqry(pc.ActivePage.Controls[0]).se do
        begin
            Lines.strings[0] := Lines.strings[0] + ',' + tvdb.Selected.Text
        end;
      end
  end;
end;

procedure Tfrmmain.FormCreate(Sender: TObject);
var
  ts1, ts2: TStrings;
  node1, node2: TTreeNode;
  i, j: integer;
begin
  qryCnt := 1;
  TAutoFree.Create(ts1, TStringList.Create());
  TAutoFree.Create(ts2, TStringList.Create());
  gini.ReadSections(ts1);
  for i := 0 to ts1.Count - 1 do
    with tvdb.items do
    begin
      Clear;
      node1 := add(nil, ts1.Strings[i]);
      gini.ReadSection(ts1.Strings[i], ts2);
      for j := 0 to ts2.Count - 1 do
        node2 := addchild(node1, ts2.names[j]);
    end;
end;

end.
